import 'package:base_lib/router/parameters.dart';
import 'package:flutter/cupertino.dart';

///发布者
abstract class IPublisher {
  void post(BuildContext context, String event, Parameters parameters,
      {String componentName});

  Future get(BuildContext context, String event, Parameters parameters,
      String componentName);
}

///订阅者:函数对象
typedef Future<Parameters> ISubscriber(BuildContext context,
    Parameters parameters);

class EventBuilder {
  final String componentName;
  final String event;
  final ISubscriber iSubscriber;

  EventBuilder(this.componentName, this.event, this.iSubscriber);

  Parameters parameters;
}

///集中式通信
///1.IEventBus继承 IPublisher,分发数据
///2.IEventBus注册和取消注册 ISubscriber
abstract class IEventBus extends IPublisher {
  void register(String event, ISubscriber subscriber,
      {String componentName, EventType type});

  void unregister(String event, ISubscriber subscriber, {String componentName});
}

///事件类型
///get:注册一个事件，可以被主动调用。同一 componentName 下只能注册一个 event 事件
///post：发送一个事件，调用其他已经注册的观察者
enum EventType { get, post }
